/*
 * Copyright (C) 2001-2010 Jacek Sieka, arnetheduck on gmail point com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#if !defined(HASH_PROGRESS_DLG_H)
#define HASH_PROGESS_DLG_H

#include "../client/HashManager.h"

class HashProgressDlg : public CDialogImpl<HashProgressDlg>
{
	public:
		enum { IDD = IDD_HASH_PROGRESS };
		enum { WM_VERSIONDATA = WM_APP + 53 };
		
		HashProgressDlg(bool aAutoClose, bool _bExitOnDone = false) : autoClose(aAutoClose), bExitOnDone(_bExitOnDone)
		{
		
		}
		~HashProgressDlg() { }
		
		BEGIN_MSG_MAP(HashProgressDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		MESSAGE_HANDLER(WM_TIMER, onTimer)
		MESSAGE_HANDLER(WM_DESTROY, onDestroy)
		COMMAND_ID_HANDLER(IDOK, OnCloseCmd)
		COMMAND_ID_HANDLER(IDCANCEL, OnCloseCmd)
		COMMAND_ID_HANDLER(IDC_BTN_ABORT, OnBnClickedBtnAbort)
		COMMAND_HANDLER(IDC_EDIT_MAX_HASH_SPEED, EN_CHANGE, OnEnChangeEditMaxHashSpeed)
		COMMAND_ID_HANDLER(IDC_PAUSE, onPause)
		END_MSG_MAP()
		
		LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
		{
			// Translate static strings
			SetWindowText(CTSTRING(HASH_PROGRESS));
			SetDlgItemText(IDOK, CTSTRING(HASH_PROGRESS_BACKGROUND));
			SetDlgItemText(IDC_STATISTICS, CTSTRING(HASH_PROGRESS_STATS));
			SetDlgItemText(IDC_HASH_INDEXING, CTSTRING(HASH_PROGRESS_TEXT));
			SetDlgItemText(IDC_BTN_ABORT, CTSTRING(HASH_ABORT_TEXT));
			SetDlgItemText(IDC_BTN_EXIT_ON_DONE, CTSTRING(EXIT_ON_HASHING_DONE_TEXT));
			SetDlgItemText(IDC_CHANGE_HASH_SPEED, CTSTRING(CHANGE_HASH_SPEED_TEXT));
			SetDlgItemText(IDC_CURRENT_HASH_SPEED, CTSTRING(CURRENT_HASH_SPEED_TEXT));
			SetDlgItemText(IDC_PAUSE, HashManager::getInstance()->isHashingPaused() ? CTSTRING(RESUME) : CTSTRING(PAUSED));
			
			ExitOnDoneButton.Attach(GetDlgItem(IDC_BTN_EXIT_ON_DONE));
			ExitOnDoneButton.SetCheck(bExitOnDone);
			
			if (!bExitOnDone)
				ExitOnDoneButton.ShowWindow(SW_HIDE);
				
			SetDlgItemInt(IDC_EDIT_MAX_HASH_SPEED, HashManager::getInstance()->GetMaxHashSpeed(), FALSE);
			
			progress.Attach(GetDlgItem(IDC_HASH_PROGRESS));
			progress.SetRange(0, HashManager::getInstance()->GetMaxProgressValue());
			updateStats();
			
			HashManager::getInstance()->setPriority(Thread::NORMAL);
			
			SetTimer(1, 1000);
			return TRUE;
		}
		
		LRESULT onPause(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
		{
			if (HashManager::getInstance()->isHashingPaused())
			{
				HashManager::getInstance()->resumeHashing();
				SetDlgItemText(IDC_PAUSE, CTSTRING(PAUSED));
			}
			else
			{
				HashManager::getInstance()->pauseHashing();
				SetDlgItemText(IDC_PAUSE, CTSTRING(RESUME));
			}
			return 0;
		}
		
		LRESULT onTimer(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
		{
			updateStats();
			return 0;
		}
		LRESULT onDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
		{
			HashManager::getInstance()->setPriority(Thread::IDLE);
			HashManager::getInstance()->DisableForceMinHashSpeed();
			progress.Detach();
			KillTimer(1);
			return 0;
		}
		
		void updateStats()
		{
			string file;
			int64_t bytes = 0;
			size_t files = 0;
			uint64_t tick = GET_TICK();
			
			HashManager::getInstance()->getStats(file, bytes, files);
			
			if (files == 0)
			{
				if (ExitOnDoneButton.GetCheck())
				{
					EndDialog(IDC_BTN_EXIT_ON_DONE);
					return;
				}
				
				if (autoClose)
				{
					PostMessage(WM_CLOSE);
					return;
				}
			}
			double diff = static_cast<double>(tick - HashManager::getInstance()->GetStartTime());
			bool paused = HashManager::getInstance()->isHashingPaused();
			if (diff < 1000 || files == 0 || bytes == 0 || paused)
			{
				SetDlgItemText(IDC_FILES_PER_HOUR, Text::toT("-.-- " + STRING(FILES_PER_HOUR) + ", " + Util::toString((uint32_t)files) + " " + STRING(FILES_LEFT)).c_str());
				SetDlgItemText(IDC_HASH_SPEED, (_T("-.-- ") + TSTRING(B) + _T("/") + TSTRING(S) + _T(", ") + Util::formatBytesW(bytes) + _T(" ") + TSTRING(LEFT)).c_str());
				if (paused)
				{
					SetDlgItemText(IDC_TIME_LEFT, Text::toT("( " + STRING(PAUSED) + " )").c_str());
				}
				else
				{
					SetDlgItemText(IDC_TIME_LEFT, Text::toT("-:--:-- " + STRING(LEFT)).c_str());
					progress.SetPos(0);
				}
			}
			else
			{
			
				int64_t processedBytes;
				size_t processedFiles;
				HashManager::getInstance()->GetProcessedFilesAndBytesCount(processedBytes, processedFiles);
				
				double filestat = (((double)(processedFiles)) * 60 * 60 * 1000) / diff;
				double speedStat = (((double)(processedBytes)) * 1000) / diff;
				
				SetDlgItemText(IDC_FILES_PER_HOUR, Text::toT(Util::toString(filestat) + " " + STRING(FILES_PER_HOUR) + ", " + Util::toString((uint32_t)files) + " " + STRING(FILES_LEFT)).c_str());
				SetDlgItemText(IDC_HASH_SPEED, (Util::formatBytesW((int64_t)speedStat) + _T("/") + TSTRING(S) + _T(", ") + Util::formatBytesW(bytes) + _T(" ") + TSTRING(LEFT)).c_str());
				
				if (EqualD(filestat, 0) || EqualD(speedStat, 0))
				{
					SetDlgItemText(IDC_TIME_LEFT, Text::toT("-:--:-- " + STRING(LEFT)).c_str());
				}
				else
				{
					double fs = static_cast<double>(files * 60 * 60) / filestat;
					double ss = bytes / speedStat;
					
					SetDlgItemText(IDC_TIME_LEFT, (Util::formatSeconds((int64_t)(fs + ss) / 2) + _T(" ") + TSTRING(LEFT)).c_str());
				}
			}
			
			if (files == 0)
			{
				SetDlgItemText(IDC_CURRENT_FILE, CTSTRING(DONE));
			}
			else
			{
				SetDlgItemText(IDC_CURRENT_FILE, Text::toT(file).c_str());
			}
			
			progress.SetPos(HashManager::getInstance()->GetProgressValue());
			SetDlgItemText(IDC_PAUSE, paused ? CTSTRING(RESUME) : CTSTRING(PAUSED)); // KUL - hash progress dialog patch
		}
		
		LRESULT OnCloseCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
		{
			EndDialog(wID);
			return 0;
		}
		
		LRESULT HashProgressDlg::OnBnClickedBtnAbort(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
		{
			HashManager::getInstance()->stopHashing(Util::emptyString);
			
			return 0;
		}
		
		LRESULT HashProgressDlg::OnEnChangeEditMaxHashSpeed(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
		{
			HashManager::getInstance()->EnableForceMinHashSpeed(GetDlgItemInt(IDC_EDIT_MAX_HASH_SPEED, NULL, FALSE));
			return 0;
		}
		
	private:
		HashProgressDlg(const HashProgressDlg&);
		
		bool autoClose;
		bool bExitOnDone;
		CProgressBarCtrl progress;
		CButton ExitOnDoneButton;
};

#endif // !defined(HASH_PROGRESS_DLG_H)

/**
 * @file
 * $Id: HashProgressDlg.h,v 1.16 2006/10/13 20:04:32 bigmuscle Exp $
 */
